from django.db import models


# Create your models here.
# 创建模型类（数据库的表）

# 新建出版社的表
class PubName(models.Model):
    pubName = models.CharField('名称', max_length=255, unique=True)


# 创建book表
class Book(models.Model):
    title = models.CharField(max_length=30, unique=True, verbose_name='书名')
    # public = models.CharField(max_length=50, verbose_name='出版社')
    price = models.DecimalField(max_digits=7, decimal_places=2, verbose_name='定价')
    retail_price = models.DecimalField(max_digits=7, decimal_places=2, verbose_name='零售价', default='30')

    # 设置外键
    pub = models.ForeignKey(to=PubName, on_delete=models.CASCADE, null=True)   # 创建Foreign外键关联pub, 以pub_id关联

    def __str__(self):
        return "title:%s pub:%s price:%s" % (self.title, self.pub, self.price)


# 创建作者表
class Author(models.Model):
    name = models.CharField(max_length=30, verbose_name='姓名')
    email = models.EmailField(verbose_name='邮箱')

    books = models.ManyToManyField(to='Book')    # 创建多对多映射关系

    def __str__(self):
        return '作者：%s 爽歪歪' % self.name


# 创建用户信息表
class UserInfo(models.Model):
    username = models.CharField(max_length=24, verbose_name='用户注册')
    password = models.CharField(max_length=24, verbose_name='密码')
